﻿
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using log4net;
using Shpm.App.Commons.Utils;
using Shpm.System.Validator;
using Shpm.System.Web.Control;
using Shpm.App.Master.Dao;

public partial class App_Input_HappyCourseSrhForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_HappyCourseSrhForm));

    
    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HappyCourseBean bean = new HappyCourseBean();
            if (getJobLevel() > 0)
            {
                bean.CommitFlg = true;
            }
            HappyCourseDao dao = new HappyCourseDao();
            IList list = dao.FindCurriculums(bean);
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();

            CommonDictionaryBean paramBean = new CommonDictionaryBean();
            CommonDictionaryDao commonDictionaryDao = new CommonDictionaryDao();

            paramBean.Catalog = "CURRICULUM_TYPE";
            IList dataList = commonDictionaryDao.FindByCatalog(paramBean);
            if (dataList.Count>0)
            {
                dataList.Insert(0,new CommonDictionaryBean());
            }

            lstActType.DataSource = dataList;
            lstActType.DataBind();

            //判断是否为审核人员
            int flg = getJobLevel();
            this.btnNew.Enabled = flg < 1;

            this.txtTeacherId.Attributes.Add("onblur", "showTeacherName(this.id,'" + this.txtTeacherName.ClientID + "');");
        }

        
    }
    // 取得审批权限
    private int getJobLevel()
    {
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
        PersonEmployeeBean beanEmp = new PersonEmployeeBean();
        HappyPlanDao dao = new HappyPlanDao();
        beanEmp.EmployeeId = emplyId;
        beanEmp = dao.GetEmployeeJob(beanEmp);
        if (beanEmp == null || string.IsNullOrEmpty(beanEmp.JobLimit))
        { return 0; }
        return Convert.ToInt32(beanEmp.JobLimit);
    }
    // before render
    void Page_PreRender(object sender, EventArgs e)
    {
        
    }

    // adjust bind data
    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HappyCourseBean bean=(HappyCourseBean)e.Row.DataItem;
            e.Row.Cells[5].Text = bean.AuditingFlg ? "已审批" : "未审批";
            e.Row.Cells[6].Text = bean.CommitFlg.Value ? "已提交" : "未提交";
        }
    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        GridView1.PageIndex = e.NewPageIndex;
        HappyCourseDao dao = new HappyCourseDao();
        HappyCourseBean bean = new HappyCourseBean();
        if (!string.IsNullOrEmpty(this.txtBeginDate.Text))
            bean.BeginDate = DateUtils.convertToDate(this.txtBeginDate.Text);
        IList list = dao.FindCurriculums(bean);
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }

    // 检索按钮
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (ComValidator1.DoValidator() == false)
        {
            ((Design_MasterPage)this.Master).InfoMessage = ComValidator1.ErrorMessage;
            return;
        }
        HappyCourseDao dao = new HappyCourseDao();
        HappyCourseBean bean = new HappyCourseBean();
        if (getJobLevel() > 0)
        {
            bean.CommitFlg = true;
        }
        bean.CurriculumName = string.IsNullOrEmpty(this.txtActName.Text.Trim()) ? null : this.txtActName.Text.Trim();
        bean.CurriculumType = string.IsNullOrEmpty(this.lstActType.SelectedValue)?null: this.lstActType.SelectedValue;
        if (!string.IsNullOrEmpty(this.txtTeacherId.Text))
            bean.TeacherId = Convert.ToInt32(this.txtTeacherId.Text);
        if (!string.IsNullOrEmpty(this.txtBeginDate.Text))
            bean.BeginDate=DateUtils.convertToDate(this.txtBeginDate.Text);
        if (!string.IsNullOrEmpty(this.txtEndDate.Text))
            bean.EndDate=DateUtils.convertToDate(this.txtEndDate.Text);
        IList list = dao.FindCurriculums(bean);
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }

    // 新建按钮
    protected void btnNew_Click(object sender, EventArgs e)
    {
        string directUrl = "~/App/Input/HappyCourseUpdForm.aspx";
        ControllerUtils.Transfer(Context, directUrl, true);
    }
        

    
    // bind common catalog data 
    private void initPageControls()
    {
        //HappyPlanDao dao = new HappyPlanDao();
        //CommonDictionaryBean beanComDict = new CommonDictionaryBean();
        //IList lstCat = null;

        //// 活动分类
        //log.Fatal("\n ***** get 活动分类 catalog *****");
        //beanComDict.Catalog = "T_ACTION_ADVICE.ADVICE_TYPE";
        //lstCat = dao.FindByCatalog(beanComDict);
        //lstActType.Items.Clear();
        //lstActType.Items.Add("");
        //bindDataToListControl("lstActType", lstCat);

        //// 审核状态
        //log.Fatal("\n ***** get 审核状态 catalog *****");
        //beanComDict.Catalog = "T_ACTION_ADVICE.DEPARTMENT_ADVICE";
        //lstCat = dao.FindByCatalog(beanComDict);
        //lstActStat1.Items.Clear();
        //lstActStat2.Items.Clear();
        //lstActStat1.Items.Add("");
        ////lstActStat1.Items.Add(new ListItem("等待","0"));
        //lstActStat2.Items.Add("");
        ////lstActStat2.Items.Add(new ListItem("等待", "0"));
        //bindDataToListControl("lstActStat1", lstCat);
        //bindDataToListControl("lstActStat2", lstCat);
    }

    // bind db data to selected controls
    private void bindDataToListControl(string ControlId, IList LstData)
    {
        if (LstData == null || LstData.Count == 0) return;

        foreach (CommonDictionaryBean bean in LstData)
        {
            if (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is DropDownList)
            {
                ((DropDownList)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
            else if ((this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is CheckBoxList)
                || (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is RadioButtonList))
            {
                ((ListControl)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int row = (sender as GridView).SelectedIndex;
        Context.Items["CURRICULUM_ID"] = (sender as GridView).DataKeys[row].Value.ToString();

        string directUrl = "~/App/Input/HappyCourseUpdForm.aspx";
        ControllerUtils.Transfer(Context, directUrl, true);
    }

}
